/* eslint-disable quotes */
function nextTick(findObj, tick) {
  function doIt() {
    var obj = findObj();
    if (!obj) {
      setTimeout(() => {
        nextTick(findObj);
      }, 200);
    }
  }
  if (tick) {
    setTimeout(doIt, tick);
  } else {
    doIt();
  }
}
// 保存明细表
const saveTableDetail = (tableContent) => {
  window.postMessage(
    {
      event: 'asyncTableDetail',
      data: tableContent
    },
    '*'
  );
};
// 处理html 片段
const dealHtmlPargram = (pargram, className = "", style = "") => {
  return `<table ${style} class="${className}">${pargram}</table>`;
};
// 返回数据
const baoBiaoDetailBack = async (data, isclose = false, lastTime = 200) => {
  window.postMessage(
    {
      event: "baoBiaoDetailBack",
      data: {
        type: data,
        isclose,
        lastTime
      }
    },
    "https://etax.zhejiang.chinatax.gov.cn"
  );
};
// 打开页面
const createNewTab = ({
  url,
  options = {},
  isAutoClose = false,
  isQsb = false
}, isdely = false) => {
  return new Promise(reslove => {
    window.postMessage(
      {
        event: "createNewTab",
        data: {
          url,
          options,
          isAutoClose,
          isQsb
        }
      },
      "*"
    );
    if (isdely) {
      setTimeout(() => {
        reslove(true);
      }, 2000);
    } else {
      reslove(true);
    }
  });
};

const asyncNewRecord = (declareType) => {
  let tableContent = {
    declareType
  };
  saveTableDetail(tableContent);
};

const asyncFb1Detail = () => {
  setTimeout(() => {
    let table1 = dealHtmlPargram(document.querySelector('body > table:nth-child(14)').innerHTML, '', 'width="1727"');
    let tableContent = {
      declareType: '增值税一般纳税人申报',
      title: '附表一',
      html: table1 + dealHtmlPargram(document.querySelector('body > table.unnamed1', 'zj-fjs-table-bordered', 'width="1727"').innerHTML),
      css: `.zj-fjs-table-bordered {border-collapse: collapse;}
      .zj-fjs-table-bordered>tbody>tr>td, .zj-fjs-table-bordered>tbody>tr>th, .zj-fjs-table-bordered>tfoot>tr>td, .zj-fjs-table-bordered>tfoot>tr>th, .zj-fjs-table-bordered>thead>tr>td, .zj-fjs-table-bordered>thead>tr>th {border: 1px solid #ddd;}`
    };
    saveTableDetail(tableContent);
  }, 500);
};
const asyncFb2Detail = () => {
  setTimeout(() => {
    let table1 = dealHtmlPargram(document.querySelector("body > table:nth-child(13)").innerHTML, '', 'width="791"');
    let tableContent = {
      declareType: '增值税一般纳税人申报',
      title: '附表二',
      html: table1 + dealHtmlPargram(document.querySelector('body > table.unnamed1', 'zj-fjs-table-bordered').innerHTML, 'zj-fjs-table-bordered', 'width="791"'),
      css: `.zj-fjs-table-bordered {border-collapse: collapse;}
        .zj-fjs-table-bordered>tbody>tr>td, .zj-fjs-table-bordered>tbody>tr>th, .zj-fjs-table-bordered>tfoot>tr>td, .zj-fjs-table-bordered>tfoot>tr>th, .zj-fjs-table-bordered>thead>tr>td, .zj-fjs-table-bordered>thead>tr>th {border: 1px solid #ddd;}`
    };
    saveTableDetail(tableContent);
  }, 500);
};
const asyncFb3Detail = () => {
  setTimeout(() => {
    let table1 = dealHtmlPargram(document.querySelector("body > table:nth-child(12)").innerHTML, '', 'width="800"');
    let tableContent = {
      declareType: '增值税一般纳税人申报',
      title: '附表三',
      html: table1 + dealHtmlPargram(document.querySelector("body > table.unnamed1").innerHTML, 'zj-fjs-table-bordered', 'width="800"'),
      css: `.zj-fjs-table-bordered {border-collapse: collapse;}
        .zj-fjs-table-bordered>tbody>tr>td, .zj-fjs-table-bordered>tbody>tr>th, .zj-fjs-table-bordered>tfoot>tr>td, .zj-fjs-table-bordered>tfoot>tr>th, .zj-fjs-table-bordered>thead>tr>td, .zj-fjs-table-bordered>thead>tr>th {border: 1px solid #ddd;}`
    };
    saveTableDetail(tableContent);
  }, 500);
};
const asyncFb4Detail = () => {
  setTimeout(() => {
    let table1 = dealHtmlPargram(document.querySelector("body > form > table:nth-child(1)"), '', 'width="800"');
    let tableContent = {
      declareType: '增值税一般纳税人申报',
      title: '附表四',
      html: table1 + dealHtmlPargram(document.querySelector('body > form > table:nth-child(2)').innerHTML, 'zj-fjs-table-bordered', 'width="800"'),
      css: `.zj-fjs-table-bordered {border-collapse: collapse;}
          .zj-fjs-table-bordered>tbody>tr>td, .zj-fjs-table-bordered>tbody>tr>th, .zj-fjs-table-bordered>tfoot>tr>td, .zj-fjs-table-bordered>tfoot>tr>th, .zj-fjs-table-bordered>thead>tr>td, .zj-fjs-table-bordered>thead>tr>th {border: 1px solid #ddd;}`
    };
    saveTableDetail(tableContent);
  }, 500);
};
const asyncJmsDetail = () => {
  setTimeout(() => {
    let table1 = dealHtmlPargram(document.querySelector('body > table:nth-child(15)').innerHTML, '', 'width="838"');
    let tableContent = {
      declareType: '增值税一般纳税人申报',
      title: '减免税明细表',
      html: table1 + dealHtmlPargram(document.querySelector('#tabList').innerHTML, 'zj-fjs-table-bordered', 'width="838"'),
      css: `.zj-fjs-table-bordered {border-collapse: collapse;}
            .zj-fjs-table-bordered>tbody>tr>td, .zj-fjs-table-bordered>tbody>tr>th, .zj-fjs-table-bordered>tfoot>tr>td, .zj-fjs-table-bordered>tfoot>tr>th, .zj-fjs-table-bordered>thead>tr>td, .zj-fjs-table-bordered>thead>tr>th {border: 1px solid #ddd;}`
    };
    saveTableDetail(tableContent);
  }, 500);
};
const asyncZzsDetail = () => {
  setTimeout(() => {
    let table1 = dealHtmlPargram(document.querySelector('body > table:nth-child(13)').innerHTML, '', 'width="780"');
    let table2 = dealHtmlPargram(document.querySelector('body > table:nth-child(14)').innerHTML, 'zj-fjs-table-bordered', 'width="791"');
    let tableContent = {
      declareType: '增值税一般纳税人申报',
      title: '增值税申报表',
      html: table1 + table2 + dealHtmlPargram(document.querySelector('body > table:nth-child(15)').innerHTML, 'zj-fjs-table-bordered', 'width="791"'),
      css: `.zj-fjs-table-bordered {border-collapse: collapse;}
            .zj-fjs-table-bordered>tbody>tr>td, .zj-fjs-table-bordered>tbody>tr>th, .zj-fjs-table-bordered>tfoot>tr>td, .zj-fjs-table-bordered>tfoot>tr>th, .zj-fjs-table-bordered>thead>tr>td, .zj-fjs-table-bordered>thead>tr>th {border: 1px solid #ddd;}`
    };
    saveTableDetail(tableContent);
  }, 500);
};
const asyncCgmxbDetail = () => {
  let table1 = dealHtmlPargram(document.querySelector("body > div.pageEnd > table:nth-child(1)").innerHTML, '', 'width="650"');
  let tableContent = {
    declareType: '增值税一般纳税人申报',
    title: '存根联明细',
    html: table1 + dealHtmlPargram(document.querySelector('body > div.pageEnd > table:nth-child(2)').innerHTML, 'zj-fjs-table-bordered', 'width="650"'),
    css: `.zj-fjs-table-bordered {border-collapse: collapse;}
          .zj-fjs-table-bordered>tbody>tr>td, .zj-fjs-table-bordered>tbody>tr>th, .zj-fjs-table-bordered>tfoot>tr>td, .zj-fjs-table-bordered>tfoot>tr>th, .zj-fjs-table-bordered>thead>tr>td, .zj-fjs-table-bordered>thead>tr>th {border: 1px solid #ddd;}`
  };
  saveTableDetail(tableContent);
};
const asyncFjsDetail = () => {
  setTimeout(() => {
    let table1 = dealHtmlPargram(document.querySelector("body > form > div > table.table.table_head").innerHTML);
    let table2 = dealHtmlPargram(document.querySelector("body > form > div > table.table.table_head2").innerHTML, 'zj-fjs-table-bordered');
    let table = document.querySelector('body > form > div > table.table.table-hover.table-bordered.table_sbb');
    console.log(table1);
    let tableContent = {
      declareType: '城建税、教育费附加、地方教育附加税（费）申报表',
      title: '申请表',
      html: table1 + table2 + dealHtmlPargram(table.innerHTML, 'zj-fjs-table-bordered'),
      css: `.zj-fjs-table-bordered {border-collapse: collapse;}
      .zj-fjs-table-bordered>tbody>tr>td, .zj-fjs-table-bordered>tbody>tr>th, .zj-fjs-table-bordered>tfoot>tr>td, .zj-fjs-table-bordered>tfoot>tr>th, .zj-fjs-table-bordered>thead>tr>td, .zj-fjs-table-bordered>thead>tr>th {border: 1px solid #ddd;}`
    };
    saveTableDetail(tableContent);
  }, 500);
};

const xgmflzlOption = () => {
  setTimeout(() => {
    let subBtn = document.querySelector("body > form > div > table.table.table_footer > tbody > tr > td > a:nth-child(2)");
    if (subBtn.innerText.includes('提')) {
      subBtn.click();
      baoBiaoDetailBack("xgmzzsflzl", true, 1000);
    }
  }, 1000);
};
const xgmjmsOption = () => {
  setTimeout(() => {
    let subBtn = document.querySelector("body > form > div > table.table.table_footer > tbody > tr > td > a:nth-child(2)");
    console.log(subBtn);
    if (subBtn.innerText.includes('提')) {
      subBtn.click();
      baoBiaoDetailBack("xgmzzsjmsmxb", true, 1000);
    }
  }, 1000);
};
const xgmssbOption = () => {
  setTimeout(() => {
    let alertBox = document.querySelector("body > div.zj-big-bg > div.zj-alert > div.zj-alert-footer > button");
    if (alertBox) alertBox.click();
    let subBtn = document.querySelector("body > form > div > table.table.table_footer > tbody > tr > td > a:nth-child(2)");
    console.log(subBtn);
    if (subBtn.innerText.includes('提')) {
      subBtn.click();
      baoBiaoDetailBack("xgmzzssbbmxb", true, 1000);
    }
  }, 1000);
};
const asyncXgmflzlDetail = () => {
  setTimeout(() => {
    let table = dealHtmlPargram(document.querySelector("body > form > div > table.table.table_head").innerHTML);
    let table1 = dealHtmlPargram(document.querySelector("body > form > div > table.table.table_head2").innerHTML);
    let table2 = dealHtmlPargram(document.querySelector("#tabList").innerHTML, 'zj-fjs-table-bordered');
    let tableContent = {
      declareType: '增值税小规模纳税人（非定期定额户）申报',
      title: ' 附列资料',
      html: table + table1 + table2,
      css: `.zj-fjs-table-bordered {border-collapse: collapse;}
      .zj-fjs-table-bordered>tbody>tr>td, .zj-fjs-table-bordered>tbody>tr>th, .zj-fjs-table-bordered>tfoot>tr>td, .zj-fjs-table-bordered>tfoot>tr>th, .zj-fjs-table-bordered>thead>tr>td, .zj-fjs-table-bordered>thead>tr>th {border: 1px solid #ddd;}`
    };
    saveTableDetail(tableContent);
  }, 500);
};
const asyncXgmjmsDetail = () => {
  setTimeout(() => {
    let table = dealHtmlPargram(document.querySelector("body > form > div > table.table.table_head").innerHTML, '', 'width="797"');
    let table1 = dealHtmlPargram(document.querySelector("#tabList").innerHTML, 'zj-fjs-table-bordered');
    let tableContent = {
      declareType: '增值税小规模纳税人（非定期定额户）申报',
      title: ' 减免税明细表',
      html: table + table1,
      css: `.zj-fjs-table-bordered {border-collapse: collapse;}
      .zj-fjs-table-bordered>tbody>tr>td, .zj-fjs-table-bordered>tbody>tr>th, .zj-fjs-table-bordered>tfoot>tr>td, .zj-fjs-table-bordered>tfoot>tr>th, .zj-fjs-table-bordered>thead>tr>td, .zj-fjs-table-bordered>thead>tr>th {border: 1px solid #ddd;}`
    };
    saveTableDetail(tableContent);
  }, 500);
};
const asyncXgmsbbDetail = () => {
  setTimeout(() => {
    let table = dealHtmlPargram(document.querySelector("body > form > div > table:nth-child(1)").innerHTML, '', 'width="800"');
    let table1 = dealHtmlPargram(document.querySelector("body > form > div > table.table.table_head2").innerHTML);
    let table2 = dealHtmlPargram(document.querySelector("#tabList").innerHTML, 'zj-fjs-table-bordered');
    let tableContent = {
      declareType: '增值税小规模纳税人（非定期定额户）申报',
      title: ' 申报表',
      html: table + table1 + table2,
      css: `.zj-fjs-table-bordered {border-collapse: collapse;}
      .zj-fjs-table-bordered>tbody>tr>td, .zj-fjs-table-bordered>tbody>tr>th, .zj-fjs-table-bordered>tfoot>tr>td, .zj-fjs-table-bordered>tfoot>tr>th, .zj-fjs-table-bordered>thead>tr>td, .zj-fjs-table-bordered>thead>tr>th {border: 1px solid #ddd;}`
    };
    saveTableDetail(tableContent);
  }, 500);
};

const qysdsA201010 = () => {
  nextTick(() => {
    let btnSubmit = document.querySelector("body > form > div > table > tbody > tr > td > table.table.table_footer > tbody > tr > td > a:nth-child(2)");
    if (btnSubmit.innerText.includes('提')) {
      btnSubmit.click();
      nextTick(() => {
        let sureBtn = document.querySelector("body > div.zj-big-bg > div.zj-alert > div.zj-alert-body");
        if (sureBtn && sureBtn.innerText.includes('成功')) {
          baoBiaoDetailBack("qysdsA201010", true, 500);
          return true;
        }
      });
    }
    return true;
  }, 200);
};

const qysdsA201020 = () => {
  nextTick(() => {
    let btnSubmit = document.querySelector("body > form > div > table > tbody > tr > td > table.table.table_footer > tbody > tr > td > a:nth-child(2)");
    if (btnSubmit.innerText.includes('提')) {
      btnSubmit.click();
      nextTick(()=> {
        let sureBtn = document.querySelector("body > div.zj-big-bg > div.zj-alert > div.zj-alert-body");
        if (sureBtn && sureBtn.innerText.includes('成功')) {
          baoBiaoDetailBack("qysdsA201010", true, 500);
          return true;
        }
      });
    }
    return true;
  }, 200);
};
const qysdsA200000 = (data) => {
  let { endPeriod, quarterSum, endSum } = data.currentZeroInfo;
  nextTick(() => {
    let arr1 = ['#qccyrs', '#qmcyrs'];
    let arr2 = ['#qczcze', '#qmzcze'];
    let jd = {
      3: 1,
      6: 2,
      9: 3,
      12: 4
    };
    arr1.forEach(item => {
      let input = $(`${item}${jd[endPeriod]}`);
      if (!input.val()) input.val(1);
      input.blur();
    });
    let input1 = $(`${arr2[0]}${jd[endPeriod]}`);
    if (parseFloat(quarterSum / 10000)) {
      input1.val(parseFloat(quarterSum / 10000));
    } else if (!input1.val()) {
      input1.val(0.01);
    }
    input1.blur();
    let input2 = $(`${arr2[1]}${jd[endPeriod]}`);
    if (parseFloat(endSum / 10000)) {
      input2.val(parseFloat(endSum / 10000));
    } else if (!input2.val()) {
      input2.val(0.01);
    }
    input2.blur();
    $('input[name="gxjsqy"]')[1].click();
    $('input[name="kjxzxqy"]')[1].click();
    $('input[name="jsrgdynssx"]')[1].click();
    let btnSubmit = document.querySelector("body > form > div > table.table.table_footer > tbody > tr > td > a:nth-child(2)");
    if (btnSubmit.innerText.includes('提')) {
      btnSubmit.click();
      nextTick(()=> {
        let sureBtn = document.querySelector("body > div.zj-big-bg > div.zj-alert > div.zj-alert-body");
        if (sureBtn && sureBtn.innerText.includes('成功')) {
          baoBiaoDetailBack("qysdsA201010", true, 500);
          return true;
        }
      });
    }
    return true;
  }, 200);
};

const qysdsA201030 = (data) => {
  nextTick(() => {
    let btnSubmit = document.querySelector("body > form > div > table.table.table_footer > tbody > tr > td > a:nth-child(2)");
    if (btnSubmit.innerText.includes('提')) {
      btnSubmit.click();
      nextTick(()=> {
        let sureBtn = document.querySelector("body > div.zj-big-bg > div.zj-alert > div.zj-alert-body");
        if (sureBtn && sureBtn.innerText.includes('成功')) {
          baoBiaoDetailBack("qysdsA201010", true, 500);
          return true;
        }
      });
    }
    return true;
  });
};

const qysdsA201010Table = async () => {
  setTimeout(() => {
    let table1 = dealHtmlPargram(document.querySelector("body > form > div > table > tbody > tr > td > table.table.table_head").innerHTML, '', 'width="100%"');
    let table2 = dealHtmlPargram(document.querySelector("body > form > div > table > tbody > tr > td > table.table.table-hover.table-bordered.table_sbb").innerHTML, 'zj-fjs-table-bordered', 'width="100%"');
    let tableContent = {
      declareType: '居民企业所得税月（季）度预缴纳税申报（A类，2018版）',
      title: 'A201010 免税收入、减计收入、所得减免等优惠明细表',
      html: table1 + table2,
      css: `.zj-fjs-table-bordered {border-collapse: collapse;}
      .zj-fjs-table-bordered>tbody>tr>td, .zj-fjs-table-bordered>tbody>tr>th, .zj-fjs-table-bordered>tfoot>tr>td, .zj-fjs-table-bordered>tfoot>tr>th, .zj-fjs-table-bordered>thead>tr>td, .zj-fjs-table-bordered>thead>tr>th {border: 1px solid #ddd;}`
    };
    saveTableDetail(tableContent);
    window.close();
  }, 500);
};
const qysdsA201020Table = async () => {
  setTimeout(() => {
    let table1 = dealHtmlPargram(document.querySelector("body > form > div > table > tbody > tr > td > table.table.table_head").innerHTML, '', 'width="100%"');
    let table2 = dealHtmlPargram(document.querySelector("body > form > div > table > tbody > tr > td > table.table.table-hover.table-bordered.table_sbb").innerHTML, 'zj-fjs-table-bordered', 'width="100%"');
    let tableContent = {
      declareType: '居民企业所得税月（季）度预缴纳税申报（A类，2018版）',
      title: 'A201020 固定资产加速折旧(扣除)优惠明细表',
      html: table1 + table2,
      css: `.zj-fjs-table-bordered {border-collapse: collapse;}
      .zj-fjs-table-bordered>tbody>tr>td, .zj-fjs-table-bordered>tbody>tr>th, .zj-fjs-table-bordered>tfoot>tr>td, .zj-fjs-table-bordered>tfoot>tr>th, .zj-fjs-table-bordered>thead>tr>td, .zj-fjs-table-bordered>thead>tr>th {border: 1px solid #ddd;}`
    };
    console.log(tableContent);
    saveTableDetail(tableContent);
    window.close();
  }, 500);
};
const qysdsA200000Table = async () => {
  setTimeout(() => {
    let table1 = dealHtmlPargram(document.querySelector("body > form > div > table.table.table_head").innerHTML, '', 'width="100%"');
    let table2 = dealHtmlPargram(document.querySelector("body > form > div > table.table.table_head2").innerHTML, '', 'width="100%"');
    let table31 = $("body > form > div > table.table.table-hover.table-bordered.table_sbb");
    // $($('input[name=gxjsqy]')[0]).attr('checked', true)
    $(table31.find('input[name=yjfs_dm]')[0]).attr('checked', true);
    $(table31.find('input[name=hz_qylb_dm]')[0]).attr('checked', true);
    $(table31.find('input[name=gxjsqy]')[1]).attr('checked', true);
    $(table31.find('input[name=kjxzxqy]')[1]).attr('checked', true);
    $(table31.find('input[name=jsrgdynssx]')[1]).attr('checked', true);
    $(table31.find('input[name=gjxzhjzhy]')[1]).attr('checked', true);
    $(table31.find('input[name=sfsyxxwlqy]')[0]).attr('checked', true);
    let table3 = dealHtmlPargram(table31.html(), 'zj-fjs-table-bordered', 'width="100%"');
    let tableContent = {
      declareType: '居民企业所得税月（季）度预缴纳税申报（A类，2018版）',
      title: 'A200000 中华人民共和国企业所得税月（季）度预缴纳税申报表（A类）',
      html: table1 + table2 + table3,
      css: `.zj-fjs-table-bordered {border-collapse: collapse;}
      .zj-fjs-table-bordered>tbody>tr>td, .zj-fjs-table-bordered>tbody>tr>th, .zj-fjs-table-bordered>tfoot>tr>td, .zj-fjs-table-bordered>tfoot>tr>th, .zj-fjs-table-bordered>thead>tr>td, .zj-fjs-table-bordered>thead>tr>th {border: 1px solid #ddd;}`
    };
    saveTableDetail(tableContent);
    window.close();
  }, 500);
};
const qysdsA201030Table = async () => {
  nextTick(() => {
    // if ($('#xxwlqy').text() === "0" || $('#xxwlqy').text()) {
    let table1 = dealHtmlPargram(document.querySelector("body > form > div > table.table.table_head").innerHTML, '', 'width="100%"');
    let table2 = dealHtmlPargram(document.querySelector("body > form > div > table.table.table-hover.table-bordered.table_sbb").innerHTML, 'zj-fjs-table-bordered', 'width="100%"');
    let tableContent = {
      declareType: '居民企业所得税月（季）度预缴纳税申报（A类，2018版）',
      title: 'A201030 减免所得税优惠明细表',
      html: table1 + table2,
      css: `.zj-fjs-table-bordered {border-collapse: collapse;}
        .zj-fjs-table-bordered>tbody>tr>td, .zj-fjs-table-bordered>tbody>tr>th, .zj-fjs-table-bordered>tfoot>tr>td, .zj-fjs-table-bordered>tfoot>tr>th, .zj-fjs-table-bordered>thead>tr>td, .zj-fjs-table-bordered>thead>tr>th {border: 1px solid #ddd;}`
    };
      // console.log(tableContent);
    saveTableDetail(tableContent);
    window.close();
    return true;
    // }
  }, 500);
};

export {
  createNewTab, baoBiaoDetailBack, nextTick,
  asyncNewRecord, asyncFb1Detail, asyncFb2Detail, asyncFb3Detail, asyncFb4Detail, asyncJmsDetail, asyncZzsDetail, asyncCgmxbDetail, asyncFjsDetail,
  xgmflzlOption, xgmjmsOption, xgmssbOption,
  asyncXgmflzlDetail, asyncXgmjmsDetail, asyncXgmsbbDetail,
  qysdsA201010, qysdsA201020, qysdsA200000, qysdsA201030, qysdsA201010Table, qysdsA201020Table, qysdsA200000Table, qysdsA201030Table
};
